<?php
	$tplPro = new XTemplate('view/product/add.htm');
	$categoryId 	= _POST('category_id');
	$productName	= _POST('product_name');
	$price			= _POST('product_price');
	$size			= _POST('size');
	$product 		= _POST('product');
	$description 	= _POST('description');		
	$tplPro->assign('baseUrl', $baseUrl);
	$error 			= '';
	if($_POST){
		
		$atHome 	= _POST('at_home');
		$atSlider 	= _POST('at_slider');
		$atPronew	= _POST('at_pronew');
		$atProsale	= _POST('at_prosale');
		$productSpecial = _POST('product_special');
		$bestseller 	= _POST('product_bestseller');
		//Update table product image
		
		if ($product['delete']) {
		
			$imgId = implode(',', $product['delete']);
			$images = $db->fetchAll("SELECT * FROM tblproduct_img WHERE img_id IN ($imgId) ");
			foreach( $images as $image ) {
				if(file_exists('..'.$image['img_url'] )){
					unlink('..'.$image['img_url']);
				}
			}
			
			$db->delete("tblproduct_img", " img_id IN ($imgId) ");
		
		}
		if($product['order']){
			
			$imgs = $product['order'];
			foreach ( $imgs as $id=>$order) {
				
				$data = array('img_order' => $order);
				$db->update('tblproduct_img', $data, " img_id = $id ");
			}
		}
		if($product['label']){
			$labels = $product['label'];
			foreach ( $labels as $id=>$label) {
				
				$data = array('img_label' => $label);
				$db->update('tblproduct_img', $data, " img_id = $id ");
			}
		}
		
		if($product['image'] ){
			
			$data = array('basic_img' => 1);
			$db->update('tblproduct_img', $data, " img_id = ".$product['image']);
		}
		
		//Validate form
		
		if($categoryId == '-1'){
			$error .= '<li>Bạn chưa chọn danh mục.</li>';
		}
		if($productName == ''){
			$error .= '<li>Bạn chưa nhập tên sản phẩm.</li>';
		}
		if($error == ''){
			$data = array();
			if(_POST('chk')){
				$relatedProduct = implode(',', _POST('chk'));
			}
			
			$data['category_id'] 	= $categoryId;
			$data['product_name'] 	= $productName;
			$data['price'] 			= $price;
			$data['size'] 			= $size;
			$data['description'] 	= $description;
			$data['at_home'] 		= $atHome ? $atHome : 0;
			$data['at_slider'] 		= $atSlider ? $atSlider :0;
			$data['at_pronew'] 		= $atPronew ? $atPronew :0;
			$data['at_prosale'] 	= $atProsale ? $atProsale :0;
			$data['special'] 		= $productSpecial ? $productSpecial :0;
			$data['bestseller'] 	= $bestseller ? $bestseller :0;
			$data['related_product']= $relatedProduct;
			$data['createddate'] 	= date("Y-m-d H:i:s");
			$data['status']			= 1;
			$productId = $db->insert('tblproduct', $data);
			$db->update('tblproduct_img', array('product_id'=>$productId), ' product_id IS NULL ');
			$db->update('tblproduct_color', array('product_id'=>$productId), ' product_id IS NULL ');
			redir('?c=product&a=list');
		}else{
			if($product['idimg']){
				$imgId = implode(',', $product['idimg']);
				$images = $db->fetchAll("SELECT * FROM tblproduct_img WHERE img_id IN ($imgId)");
				foreach ( $images as $image ) {
					//checked="checked"
					if($image['thumbnail'] == 1){
						$image['thumbcheck'] = 'checked="checked"';
					}
					if($image['small_img'] == 1){
						$image['smallcheck'] = 'checked="checked"';
					}
					if($image['basic_img'] == 1){
						$image['basecheck'] = 'checked="checked"';
					}
					$tplPro->insert_loop('add.listimg', array('listimg'=>$image));
				}
			}
			$tplPro->assign('productName', $productName);
			$tplPro->assign('price', $price);
			$tplPro->assign('size', $size);
			$tplPro->assign('description', $description);
			//$tplPro->assign('error', $error);
			//$tplPro->assign('error', $error);
			$tplPro->assign('error', $error);
		}	
	}else{
		//Remove all product_img don't have product_id
		$images = $db->fetchAll('SELECT * FROM tblproduct_img WHERE (product_id IS NULL) OR (product_id = 0)');
		foreach( $images as $image ) {
			if(file_exists('..'.$image['img_url'] )){
				unlink('..'.$image['img_url']);
			}
		}
		$delete = $db->delete('tblproduct_img', ' (product_id IS NULL) OR (product_id = 0)');
		
		//Product image color
		$images = $db->fetchAll('SELECT * FROM tblproduct_color WHERE (product_id IS NULL) OR (product_id = 0)');
		foreach( $images as $image ) {
			if(file_exists('..'.$image['img_url'] )){
				unlink('..'.$image['img_url']);
			}
		}
		$delete = $db->delete('tblproduct_color', ' (product_id IS NULL) OR (product_id = 0) ');
		
	}
	
	$sltCategory = getCombo('tblcategory', 'category_id', 'category_name', 'category_id', '', null, null, null, 'input-text-medium');
	$sltCategoryS = getCombo('tblcategory', 'category_id', 'category_name', 's_category_id', '', null, null, null, 'field-175');
	$tplPro->assign('sltCategoryS', $sltCategoryS);
	$tplPro->assign('sltCategory', $sltCategory);
	
	$tplPro->parse('add');
	$content = $tplPro->text('add');